package com.logistics.socket.service;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.logistics.delivery.service.DeliveryService;
import com.logistics.heart.service.HeartService;
import com.logistics.socket.model.*;
import com.logistics.utils.constants.Constants;
import com.logistics.utils.socket.EventData;
import com.logistics.utils.socket.WebSocketManagerClient;
import com.logistics.utils.tool.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 给AGV调度平台发送请求SOCKET服务
 */
@Service
public class RequestSocketService {

    @Autowired
    private DeliveryService deliveryService;

    @Autowired
    @Lazy
    private HeartService heartService;

    @Autowired(required = false)
    private WebSocketManagerClient client;

    /**
     * 像AGV调度系统发消息
     */
    public void sendMessage(EventData<RequestTaskModel> request) throws JsonProcessingException{
        switch (request.getEventName()){
            case "__heart": sendHeart(); break;
            case "__task_scheduling": genAgvSchedulingTask(request.getData()); break;
        }
    }

    /**
     * 发送心跳
     */
    private void sendHeart() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        client.send(mapper.writeValueAsString(RequestSocketModel. build(Constants.TYPE_HEART)));
    }

    /**
     * 任务生成
     */
    public void genAgvSchedulingTask(RequestTaskModel param) throws JsonProcessingException {
        RequestSocketModel request = RequestSocketModel.build(Constants.TYPE_SCHEDULING_TASK);
        RequestValueModel requestValueModel = null;
        switch (param.getType()){
            case "0":
                // 空车呼叫 需要手动释放任务
                heartService.updateStateByIp(Constants.HANDLER_STATE_EXECUTE_TASK, param.getDeviceId());
                // 点到点配送
                requestValueModel = RequestValueModel.pointToPoint(param.getDeviceId(), param.getEndStation());
                break;
            case "1":
                // 货物配送
                requestValueModel = RequestValueModel.delivery(param.getDeviceId(), param.getStartStation(), param.getEndStation());
                break;
            case "2":
                // 空托呼叫
                requestValueModel = RequestValueModel.containerCall(param.getStartStation(), param.getEndStation(),
                        param.getLoadHeight(), param.getContainHeight());
                break;
            case "3":
                // 空托回收
                requestValueModel = RequestValueModel.containerRecovery(param.getDeviceId(),
                        param.getStartStation(), param.getEndStation(), param.getUnloadHeight(), param.getUnloadDownloadHeight());
                break;
            case "4":
                // 托盘搭载
                requestValueModel = RequestValueModel.containerCarrying(param.getDeviceId(), param.getEndStation());
                break;
            case "5":
                // 托盘放置
                requestValueModel = RequestValueModel.placeTask(param.getDeviceId(), param.getEndStation());
                break;
            default:
                break;
        }
        List<RequestValueModel> list = new ArrayList<>();
        list.add(requestValueModel);
        request.setData(RequestSchedulingModel.build(RequestKeyModel.instance(), list));
        System.out.println("====" + JSONObject.toJSONString(request));
        ObjectMapper mapper = new ObjectMapper();
        if(StringUtils.isNotNull(param.getDeliveryId())){
            deliveryService.updateReqCodeById(request.getReqCode(), Integer.parseInt(param.getDeliveryId()));
        }
        client.send(mapper.writeValueAsString(request));
    }
}
